﻿#region License


// Virtual Essentials - http://www.Virtual-Essentials.com
// Copyright (c) 2005-2013
// 
//  This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DotNetNuke.Data;
using DotNetNuke.Collections;


namespace VirtualEssentials.DNN.Modules.Testimonials
{
    public class DataProvider
    {
        #region Shared/Static Methods

        // singleton reference to the instantiated object 

        private static readonly DataProvider provider;

        // constructor
        static DataProvider()
        {
            provider = new DataProvider();
        }

        // return the provider
        public static DataProvider Instance()
        {
            return provider;
        }

        #endregion

        #region DAL2
        public IEnumerable<TestimonialInfo> GetTestimonials(int ModuleId)
        {
            IEnumerable<TestimonialInfo> testimonials;

            using (IDataContext ctx = DataContext.Instance())
            {
                var rep = ctx.GetRepository<TestimonialInfo>();
                testimonials = rep.Find("WHERE ModuleID = @0", ModuleId);
            }
            return testimonials;
        }

        /// <summary>
        /// retrieves a paged set of testimonials
        /// </summary>
        /// <param name="ModuleId"></param>
        /// <param name="Index"></param>
        /// <param name="Size"></param>
        /// <returns></returns>
        public IPagedList<TestimonialInfo> GetPagedTestimonials(int ModuleId, int Index, int Size)
        {
            IPagedList<TestimonialInfo> testimonials;

            using (IDataContext ctx = DataContext.Instance())
            {
                var rep = ctx.GetRepository<TestimonialInfo>();
                testimonials = rep.GetPage(ModuleId, Index, Size);
            }
            return testimonials;
        }

        /// <summary>
        /// retrieves all published testimonials
        /// </summary>
        /// <param name="ModuleId"></param>
        /// <returns></returns>
        public IEnumerable<TestimonialInfo> GetPublishedTestimonials(int ModuleId, string OrderBy, int NumberToDisplay)
        {
           // IEnumerable<TestimonialInfo> testimonials;

            using (IDataContext ctx = DataContext.Instance())
            {
                var rep = ctx.GetRepository<TestimonialInfo>();
                var testimonials = rep.Find("WHERE IsPublished = @0 AND ModuleID = @1", true, ModuleId);

                switch (OrderBy) {
                    case "TestimonialDateAsc":
                       testimonials = testimonials.OrderBy(a => a.TestimonialDate);
                        break;
                    case "TestimonialDateDesc":
                        testimonials = testimonials.OrderByDescending(a => a.TestimonialDate);
                        break;
                    case "AuthorFirstNameAZ":
                        testimonials = testimonials.OrderBy(a => a.AuthorFirstName);
                        break;
                    case "AuthorFirstNameZA":
                        testimonials = testimonials.OrderByDescending(a => a.AuthorFirstName);
                        break;
                    case "AuthorLastNameAZ":
                        testimonials = testimonials.OrderBy(a => a.AuthorLastName);
                        break;
                    case "AuthorLastNameZA":
                        testimonials = testimonials.OrderByDescending(a => a.AuthorLastName);
                        break;
                    case "AuthorLocationAZ":
                        testimonials = testimonials.OrderBy(a => a.AuthorLocation);
                        break;
                    case "AuthorLocationZA":
                        testimonials = testimonials.OrderByDescending(a => a.AuthorLocation);
                        break;
                    case "AuthorTitleAZ":
                        testimonials = testimonials.OrderBy(a => a.AuthorTitle);
                        break;
                    case "AuthorTitleZA":
                        testimonials = testimonials.OrderByDescending(a => a.AuthorTitle);
                        break;
                    case "AuthorWebsiteAZ":
                        testimonials = testimonials.OrderBy(a => a.AuthorWebsite);
                        break;
                    case "AuthorWebsiteZA":
                        testimonials = testimonials.OrderByDescending(a => a.AuthorWebsite);
                        break;
                    case "TestimonialAZ":
                        testimonials = testimonials.OrderBy(a => a.Testimonial);
                        break;
                    case "TestimonialZA":
                        testimonials = testimonials.OrderByDescending(a => a.Testimonial);
                        break;
                 }

                testimonials = testimonials.Take(NumberToDisplay).ToList();
                return testimonials; 
            
            }
        }
        
        /// <summary>
        /// retrieves all published testimonials
        /// </summary>
        /// <param name="ModuleId"></param>
        /// <returns></returns>
        public IEnumerable<TestimonialInfo> GetUnpublishedTestimonials(int ModuleId)
        {
            IEnumerable<TestimonialInfo> testimonials;

            using (IDataContext ctx = DataContext.Instance())
            {
                var rep = ctx.GetRepository<TestimonialInfo>();
                testimonials = rep.Find("WHERE IsPublished = @0 AND ModuleID = @1", false, ModuleId).OrderBy(a => a.TestimonialDate).ThenByDescending(a => a.TestimonialDate);
            }
            return testimonials;
        }
        /// <summary>
        /// Adds a new testimonial
        /// </summary>
        /// <param name="testimonial"></param>
        public void AddNewTestimonial(TestimonialInfo testimonial)
        {
            using (IDataContext ctx = DataContext.Instance())
            {
                var rep = ctx.GetRepository<TestimonialInfo>();
                rep.Insert(testimonial);
            }
        }

        /// <summary>
        /// Removes a testimonial from the database
        /// </summary>
        /// <param name="testimonial"></param>
        public void DeleteTestimonial(int TestimonialID)
        {
            using (IDataContext ctx = DataContext.Instance())
            {
                var rep = ctx.GetRepository<TestimonialInfo>();
                rep.Delete("WHERE TestimonialID = @0", TestimonialID);
            }
        }

        /// <summary>
        /// Removes all testimonials for a given module
        /// </summary>
        /// <param name="ModuleID"></param>
        public void DeleteAllTestimonials(int ModuleID)
        {
            using (IDataContext ctx = DataContext.Instance())
            {
                var rep = ctx.GetRepository<TestimonialInfo>();
                rep.Delete("WHERE ModuleID = @0", ModuleID);
            }
        }

        /// <summary>
        /// Publish a Testimonial
        /// </summary>
        /// <param name="TestimonialID"></param>
        /// <param name="PublishFlag"></param>
        public void PublishTestimonial(int TestimonialID, bool PublishFlag)
        {
            using (IDataContext ctx = DataContext.Instance())
            {
                var rep = ctx.GetRepository<TestimonialInfo>();
                var item = rep.GetById(TestimonialID);
                item.IsPublished = PublishFlag;
                rep.Update(item);
            }
        }

       #endregion
    }
}